﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Undercover.Model
{
    public class UserLibrary
    {
        public static Lazy<UserLibrary> Global = new Lazy<UserLibrary>(() => UserLibrary.Load(App.UserLibraryPath), false);

        public WordPairList WordPairs { get; private set; }


        /// <summary>
        /// The use count of the word from the selected packages.
        /// </summary>
        public int UseCount
        {
            get
            {
                return WordPairs.Count(it => it.UseCount > 0);
            }
        }

        /// <summary>
        /// The total count of the word from the selected packages.
        /// </summary>
        public int TotalCount
        {
            get
            {
                return WordPairs.Count;
            }
        }

        public WordPair PickUp()
        {
            var pairs = WordPairs.Where(it => it.UseCount==0).ToArray();

            if (pairs.Length ==0 ) 
            {
                return null;
            }

            return pairs[random.Next(pairs.Length)];
        }

        public static UserLibrary Load(string path)
        {
            UserLibrary ret = new UserLibrary();

            // load from local storage
            try
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, FileMode.Open, FileAccess.Read, file))
                {
                    var reader = new StreamReader(stream);
                    XmlSerializer xs = new XmlSerializer(typeof(WordPairList));
                    ret.WordPairs = xs.Deserialize(reader) as WordPairList;
                }
            }
            catch { }
            finally { }

            // generate a new default one, just for testing. the following code should be removed
            if (ret.WordPairs == null)
            {
                ret.WordPairs = new WordPairList();
                ret.WordPairs.Add(new Undercover.Model.WordPair() { MajorityWord = "猪头", MinorityWord = "白菜", CanSwap = true });
                ret.WordPairs.Add(new Undercover.Model.WordPair() { MajorityWord = "鼠标", MinorityWord = "键盘", CanSwap = false });
            }

            return ret;
        }

        public void Save(string path)
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path,
                FileMode.Create, FileAccess.Write, file))
            {
                XmlSerializer xml = new XmlSerializer(typeof(WordPairList));
                xml.Serialize(stream, WordPairs);
            }
        }

        private static Random random = new Random((int)DateTime.UtcNow.Ticks);
    }
}
